<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/12/16
 * Time: 17:05
 */
require 'config.php';
switch (@$_GET['a']){
    case 'login':

        if(!empty($_POST['uname']) && !empty($_POST['pwd'])){
            $uname = $_POST['uname'];
            $pwd = $_POST['pwd'];
            $sql = "select * from user where userName='{$uname}' and password='{$pwd}' and status=1 and auth <2";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            $id = $rows['id'];
            if ($rows){
                if ($rows['auth'] ==1 ){
                    setcookie('auth1','会员用户',time()+3600,'/');
                    setcookie('uname1',$uname,time()+3600*24,'/');
                    setcookie('id1',$id,time()+3600*24,'/');
                    echo "<script>alert('登录成功');window.location.href='../index.php';</script>";die;
                }elseif ($rows['auth'] ==0) {
                    setcookie('auth1','普通用户',time()+3600*24,'/');
                    setcookie('uname1',$uname,time()+3600,'/');
                    setcookie('id1',$id,time()+3600*24,'/');
                    echo "<script>alert('登录成功');window.location.href='../index.php';</script>";die;
                }
            }else{
                echo "<script>alert('用户名不存在');window.location.href='../index.php';</script>";die;
            }
        }else{
            echo "<script>alert('内容不能为空');window.location.href='../index.php';</script>";die;
        }
        break;
    case 'yan':
        if(!empty($_POST['zname']) && !empty($_POST['zpwd']) && !empty($_POST['yanzheng'])){
            $uname = $_POST['zname'];
            $pwd = $_POST['zpwd'];
            $yan = $_POST['yanzheng'];
            $cook = $_COOKIE['coke'];
            $sql = "select * from user where userName='{$uname}' and password='{$pwd}' and status=1 and auth <2";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            $id = $rows['id'];
            if ($rows){
                if ($rows['auth'] ==1 ){
                    setcookie('auth1','会员用户',time()+3600,'/');
                    setcookie('uname1',$uname,time()+3600*24,'/');
                    setcookie('id1',$id,time()+3600*24,'/');
                    if($yan == $cook){
                        echo "<script>alert('登录成功');window.location.href='../index.php';</script>";die;
                    }else{
                        echo "<script>alert('验证码错误');window.location.href='./register.php';</script>";die;
                    }

                }elseif ($rows['auth'] ==0) {
                    setcookie('auth1','普通用户',time()+3600*24,'/');
                    setcookie('uname1',$uname,time()+3600,'/');
                    setcookie('id1',$id,time()+3600*24,'/');
                    if($yan == $cook){
                        echo "<script>alert('登录成功');window.location.href='../index.php';</script>";die;
                    }else{
                        echo "<script>alert('验证码错误');window.location.href='./register.php';</script>";die;
                    }
                }
            }
        }
        break;
    case 'exit':
        $time = date('Y-m-d H:i:s');
        $uname = $_COOKIE['uname'];
        $sql ="update user set lastlogin = '{$time}' where userName = '{$uname}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            setcookie('uname1','',time()-1,'/');
            setcookie('auth1','',time()-1,'/');
            setcookie('id1','',time()-1,'/');
            echo "<script>alert('退出成功');window.location.href='../index.php';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'register';
    if (!empty($_POST['zname'])&&!empty($_POST['zpwd'])&&!empty($_POST['zmail'])){
        $zname = $_POST['zname'];
        $zpwd =  $_POST['zpwd'];
        $zuser = @$_POST['zuser'];
        $zmail = @$_POST['zmail'];
        $zqq = @$_POST['zqq'];
        $zage = @$_POST['zage'];
        $zsex = @$_POST['zsex'];
        $zcontent = @$_POST['zcontent'];
        $sql = "insert into user values(null,'{$zname}','{$zpwd}','0','1','','')";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
           $sql1 = "select id from user where userName = '{$zname}'";
           $result = mysqli_query($link,$sql1);
           $rows = mysqli_fetch_assoc($result);
           $id = $rows['id'];
           $sql2 = "insert into userdetail values (null,$id,'$zuser','$zmail','$zqq','$zage','$zsex','','','$zcontent','./images/tou.gif')";
           mysqli_query($link,$sql2);
           if (mysqli_affected_rows($link)>0){
               $sql3 = "insert into jifen values (null,$id,'','')";
               mysqli_query($link,$sql3);
               if (mysqli_affected_rows($link)>0) {
                   echo "<script>alert('注册成功');window.location.href='../index.php';</script>";
                   die;
               }
           }

        }else{
            echo "<script>alert('内容填写错误或用户名重复');window.location.href='./register.php';</script>";die;
        }
        mysqli_free_result($result);
        mysqli_close($link);
    }else{
        echo "<script>alert('内容不能为空');window.location.href='./register.php';</script>";die;
    }
    break;


}